package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.ShBannerPicture;
import com.ruoyi.system.service.IShBannerPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/bannerpic")
public class ShBannerPictureController extends BaseController {
    @Autowired
    private IShBannerPictureService shBannerPictureService;

    /**
     * 查询商品图片列表
     */
    @RequiresPermissions("system:bannerpic:list")
    @GetMapping("/list")
    public TableDataInfo list(ShBannerPicture shBannerPicture)
    {
        startPage();
        List<ShBannerPicture> list = shBannerPictureService.selectShBannerPictureList(shBannerPicture);
        return getDataTable(list);
    }

    /**
     * 导出商品图片列表
     */
    @RequiresPermissions("system:bannerpic:export")
    @Log(title = "商品图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShBannerPicture shBannerPicture)
    {
        List<ShBannerPicture> list = shBannerPictureService.selectShBannerPictureList(shBannerPicture);
        ExcelUtil<ShBannerPicture> util = new ExcelUtil<ShBannerPicture>(ShBannerPicture.class);
        util.exportExcel(response, list, "商品图片数据");
    }

    /**
     * 获取商品图片详细信息
     */
    @RequiresPermissions("system:bannerpic:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shBannerPictureService.selectShBannerPictureById(id));
    }

    @GetMapping(value = "/bpic/{bannerId}")
    public AjaxResult getBannerPic(@PathVariable("bannerId") Long bannerId)
    {
        ShBannerPicture param = new ShBannerPicture();
        param.setBannerId(bannerId);
        param.setIsVideo(0);
        List<ShBannerPicture> list = shBannerPictureService.selectShBannerPictureList(param);
        if (null!=list&&list.size()>0) {
            return success(list.get(0));
        }else {
            return success(null);
        }
    }

    /**
     * 新增商品图片
     */
    @RequiresPermissions("system:bannerpic:add")
    @Log(title = "商品图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShBannerPicture shBannerPicture)
    {
        return toAjax(shBannerPictureService.insertShBannerPicture(shBannerPicture));
    }

    @PostMapping("/add")
    public R<Integer> addBannerPic(@RequestBody ShBannerPicture shBannerPicture)
    {
        return R.ok(shBannerPictureService.insertShBannerPicture(shBannerPicture));
    }

    @PostMapping("/del/{id}")
    public R<Integer> delBannerPic(@PathVariable Long id)
    {
        return R.ok(shBannerPictureService.deleteShBannerPictureById(id));
    }

    /**
     * 修改商品图片
     */
    @RequiresPermissions("system:bannerpic:edit")
    @Log(title = "商品图片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShBannerPicture shBannerPicture)
    {
        return toAjax(shBannerPictureService.updateShBannerPicture(shBannerPicture));
    }

    /**
     * 删除商品图片
     */
    @RequiresPermissions("system:bannerpic:remove")
    @Log(title = "商品图片", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shBannerPictureService.deleteShBannerPictureByIds(ids));
    }
}
